
package br.com.aeternu.controleatendimento.manutencao.conclusao.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.apache.log4j.Logger;
import br.com.aeternu.controleatendimento.common.ui.EasyJFrame;
import br.com.aeternu.controleatendimento.common.ui.TableCellListener;

public class UIConclusao extends EasyJFrame
{

   private static final long serialVersionUID = 1L;
   private static Logger logger = Logger.getLogger(UIConclusao.class);
   private JButton btnNovaConclusao;
   private JPanel contentPane;
   private IfcUIConclusao listener;
   private JScrollPane scrollPane;
   private JTable table;

   Action action = new AbstractAction()
   {

      private static final long serialVersionUID = 1L;

      public void actionPerformed(ActionEvent e)
      {
         TableCellListener tcl = (TableCellListener) e.getSource();
         logger.info("Row   : " + tcl.getRow());
         logger.info("Column: " + tcl.getColumn());
         logger.info("Old   : " + tcl.getOldValue());
         logger.info("New   : " + tcl.getNewValue());

         if (tcl.getOldValue() != tcl.getNewValue())
            atualizaPlanilha(tcl.getRow(), tcl.getColumn(), tcl.getNewValue());
      }
   };

   public UIConclusao(IfcUIConclusao listener)
   {
      super("Conclusões");

      this.listener = listener;

      new TableCellListener(getTable(), action);

      initComponents();
   }

   public JButton getBtnNovaConclusao()
   {
      if (btnNovaConclusao == null)
      {
         btnNovaConclusao = new JButton("Criar Conclusão");
         btnNovaConclusao.setMnemonic('C');
         btnNovaConclusao.addActionListener(new ActionListener()
         {

            public void actionPerformed(ActionEvent e)
            {
               listener.incluirConclusao();
            }
         });
      }
      return btnNovaConclusao;
   }

   public JScrollPane getScrollPane()
   {
      if (scrollPane == null)
      {
         scrollPane = new JScrollPane();
         scrollPane.setViewportView(getTable());
      }
      return scrollPane;
   }

   public JTable getTable()
   {
      if (table == null)
      {
         table = new JTable();
         table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
         table.setModel(getTableModel());
         table.getColumnModel().getColumn(0).setPreferredWidth(200);
         table.getColumnModel().getColumn(0).setMinWidth(100);
      }
      return table;
   }

   @Override
   public void sair()
   {
      dispose();
   }

   private void atualizaPlanilha(int row, int column, Object newValue)
   {
      listener.atualizaPlanilha(this, row, column, newValue);
   }

   private TableModel getTableModel()
   {
      return new DefaultTableModel(
         new Object[][]{
            {null},
         },
         new String[]{
            "Conclus\u00E3o"
         })
      {

         private static final long serialVersionUID = 1L;
         Class<?>[] columnTypes = new Class[]{
            String.class
         };

         public Class<?> getColumnClass(int columnIndex)
         {
            return columnTypes[columnIndex];
         }
      };
   }

   private void initComponents()
   {
      setResizable(false);
      setBounds(100, 100, 360, 300);
      contentPane = new JPanel();
      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
      setContentPane(contentPane);
      GroupLayout gl_contentPane = new GroupLayout(contentPane);
      gl_contentPane.setHorizontalGroup(
               gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addComponent(getScrollPane(), Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                 .addComponent(getBtnNovaConclusao())
                                 .addContainerGap())
               );
      gl_contentPane.setVerticalGroup(
               gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                 .addComponent(getScrollPane(), GroupLayout.PREFERRED_SIZE, 233, GroupLayout.PREFERRED_SIZE)
                                 .addPreferredGap(ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                                 .addComponent(getBtnNovaConclusao()))
               );
      contentPane.setLayout(gl_contentPane);
      setVisible(true);
   }
}
